//! 实用工具模块

mod qemu;
mod signature;
mod fs_util;
mod cgroups;
mod namespaces;

pub use qemu::{ArchType, QemuManager, CrossEnvBuilder};
pub use signature::{sign_file, verify_signature};
pub use fs_util::{secure_delete, checksum_file};
pub use cgroups::{CGroupController, ResourceLimit};
pub use namespaces::{unshare_mount, setup_veth};

use crate::core::error::AppError;

/// 系统架构检测
pub fn detect_host_arch() -> Result<ArchType, AppError> {
    let output = std::process::Command::new("uname")
        .arg("-m")
        .output()?;
    
    String::from_utf8(output.stdout)?
        .trim()
        .parse()
}

/// 安全执行命令（带权限验证）
pub async fn privileged_exec(cmd: &mut Command) -> Result<(), AppError> {
    use nix::unistd::{Uid, Gid};
    
    if !Uid::effective().is_root() {
        return Err(AppError::PermissionDenied("需要root权限".into()));
    }

    // 降权执行
    cmd.uid(Some(1000))
       .gid(Some(1000))
       .status()?;
    
    Ok(())
}

/// 跨平台路径转换
#[cfg(windows)]
pub fn to_unix_path(path: &str) -> String {
    path.replace('\\', "/")
}

#[cfg(not(windows))]
pub fn to_unix_path(path: &str) -> String {
    path.to_string()
}